import 'package:flutter/material.dart';
import 'package:get/get.dart';
//import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/dialogs/cn_custom_dialog.dart';
import 'package:znmm/UI/LoginPages/mag/user_info_manage.dart';
//import 'package:glassesin/UI/A_Common/email/edti_temp/edti_temp_page.dart';
import 'package:znmm/config/app_asset.dart';
import 'package:znmm/config/app_color.dart';
import 'package:znmm/config/app_init.dart';
import '../LoginPages/mag/login_mag.dart';
import 'manage/cn_user_mag.dart';
import 'model/option_model.dart';
import 'view/cn_aboutUs_view.dart';
import 'view/cn_basicinfo_view.dart';
import 'view/cn_billingedit_view.dart';
import 'view/cn_shippingedit_view.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  List<OptionModel> _optionsList = [];

  @override
  void initState() {
    super.initState();
    CnUserMag().reqUserInfo();

    _optionsList = [
      OptionModel(
          icon: Icons.settings_applications_outlined,
          title: 'Settings',
          iType: 0),
      OptionModel(
          icon: Icons.attach_money_outlined, title: 'Billing Info', iType: 1),
      OptionModel(
          icon: Icons.shop_2_outlined, title: 'Shipping Info', iType: 2),
      OptionModel(icon: Icons.toll, title: 'About Us', iType: 3),
      OptionModel(
          icon: Icons.delete_forever, title: 'Delete Account', iType: 4),
      OptionModel(icon: Icons.exit_to_app, title: 'Logout', iType: 5),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.backgroundColor,
      body: Column(
        children: [
          // Custom AppBar
          Container(
            padding:
                const EdgeInsets.only(top: 64, left: 16, right: 16, bottom: 20),
            decoration: const BoxDecoration(
              color: AppColor.whiteColor,
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(20),
                bottomRight: Radius.circular(20),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // IconButton(
                //   icon: const Icon(Icons.arrow_back, color: Colors.white),
                //   onPressed: () => Navigator.pop(context),
                // ),
                // const SizedBox(
                //   width: 40,
                // ),
                const CircleAvatar(
                  radius: 40,
                  backgroundColor: AppColor.backgroundColor,
                  backgroundImage: AssetImage(
                      AppAsset.imageLogo), // Replace with your image asset
                ),
                const SizedBox(width: 16),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      !UserInfoMag().bIsVisito
                          ? UserInfoMag().display_name
                          : "Visitor",
                      style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: AppColor.black),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      UserInfoMag().email,
                      style: TextStyle(
                        fontSize: 14,
                        color: AppColor.black,
                      ),
                    ),
                  ],
                ),
                const Spacer(),
                // IconButton(
                //   icon: Icon(Icons.settings, color: AppColor.black),
                //   onPressed: () {
                //     // Handle settings button press
                //   },
                // ),
              ],
            ),
          ),

          // Body Content
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  MediaQuery.removePadding(
                      removeTop: true,
                      context: context,
                      child: ListView.builder(
                        physics: const NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: _optionsList.length,
                        itemBuilder: (context, index) {
                          OptionModel pmodel = _optionsList[index];
                          return ListTile(
                            leading: Icon(pmodel.icon, color: AppColor.black),
                            title: Text(pmodel.title),
                            trailing: const Icon(Icons.arrow_forward_ios,
                                size: 16, color: AppColor.grey),
                            onTap: () async {
                              if (pmodel.iType == 0) {
                                if (UserInfoMag().bIsVisito) {
                                  _showTips();
                                } else {
                                  final result =
                                      await Get.to(const CnBasicInfoView());
                                  if (result == true) {
                                    setState(() {});
                                  }
                                }
                              } else if (pmodel.iType == 1) {
                                if (UserInfoMag().bIsVisito) {
                                  _showTips();
                                } else {
                                  final result =
                                      await Get.to(const CnBillingEditView());
                                  if (result == true) {
                                    setState(() {});
                                  }
                                }
                              } else if (pmodel.iType == 2) {
                                if (UserInfoMag().bIsVisito) {
                                  _showTips();
                                } else {
                                  final result =
                                      await Get.to(const CnShippingEditView());
                                  if (result == true) {
                                    setState(() {});
                                  }
                                }
                              } else if (pmodel.iType == 3) {
                                Get.to(const CnAboutUsView());
                              } else if (pmodel.iType == 4) {
                                if (UserInfoMag().bIsVisito) {
                                  _showTips();
                                } else {
                                  showDialog(
                                    context: context,
                                    builder: (context) => CustomDialog(
                                      //title: 'Confirmation',
                                      content: 'Delete Account?',
                                      onConfirm: () {
                                        LoginMag().reqDeleteCustomers();
                                      },
                                    ),
                                  );
                                }
                              } else {
                                showDialog(
                                  context: context,
                                  builder: (context) => CustomDialog(
                                    content: 'Logout?',
                                    onConfirm: () {},
                                  ),
                                ).then((value) {
                                  if (value != null && value) {
                                    AppInit.logoutDispose();
                                  }
                                });
                              }
                            },
                          );
                        },
                      ))
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _showTips() {
    showDialog(
      context: context,
      builder: (context) => const CustomDialog(
        content: 'Please Login?',
        confirmText: "Login",
      ),
    ).then((value) {
      if (value != null && value) {
        AppInit.logoutDispose();
      }
    });
  }
}
